// 课程数据列
const list = document.querySelector('.list')
// 学生数据列
const studentsList = document.querySelector('.courseStudents .studentsList')

// 获取UID
const UID = localStorage.getItem('UID')

// 教师姓名
const teacherName = document.querySelector('.teacherName')
// 总评
const courseNumber = document.querySelector('.courseNumber')
// 已评价和未评价
const courseYes = document.querySelector('.courseYes')
const courseNo = document.querySelector('.courseNo')

// 表格控制栏
const first = document.querySelector('.first')
const last = document.querySelector('.last')
const next = document.querySelector('.next')
const theLast = document.querySelector('.theLast')
const paged = document.querySelector('.paged')
const pageSum = document.querySelector('.controlTable select')
const allPage = document.querySelector('.allPage')


let nowClassId      // 当前选中课程
let classCredit        // 当前选中课程的学分

// 页码
let page = 1
let pageNumber = 3
// 记录最大页数
let maxPage = 0


async function getAllCourse() {
  const result = (await axios({
    url: 'http://127.0.0.1:8080/Teacher/SupplementalGrades/getAllCourse',
    params: {
      UID
    }
  })).data.data
  const htmlStr = await Promise.all(result.map(async (e) => {
    const haveSupplePeople = await getHaveSupplePeople(e.id)
    return `
      <tr data-id="${e.id}">
        <td>${e.id}</td>
        <td>${e.name}</td>
        <td>${e.credit}</td>
        <td>${haveSupplePeople}/${e.peopleNumber}</td>
      </tr>
    `
  }))
  list.innerHTML = htmlStr.join('')
}

getAllCourse()

// 获取已选课人数
async function getHaveSupplePeople(classId) {
  const result = (await axios({
    url: 'http://127.0.0.1:8080/Teacher/SupplementalGrades/getHaveSupplePeople',
    params: {
      id: classId + ''
    }
  })).data.data
  return result
}

// 根据课程号获取所有课程
async function getCourseStudent(classId) {
  return (await axios({
    url: 'http://127.0.0.1:8080/Teacher/SupplementalGrades/getAllStudentInformationByClassId',
    params: {
      id: classId,
      page,
      pageNumber
    }
  })).data.data
}

// 获取教师信息
async function getTeacherName() {
  const result = (await axios({
    url: 'http://127.0.0.1:8080/Teacher/PersonData/Information',
    params: {
      id: UID
    }
  })).data.data
  teacherName.innerHTML = result.name
}

// 获取课程评分信息
async function getClassSuppleInformation() {
  const allCourse = (await axios({
    url: 'http://127.0.0.1:8080/Teacher/SupplementalGrades/getAllCourseCount',
    params: {
      UID
    }
  })).data.data
  courseNumber.innerHTML = allCourse
  const haveSuppleCourseCount = (await axios({
    url: 'http://127.0.0.1:8080/Teacher/SupplementalGrades/haveSuppleCourseCount',
    params: {
      UID
    }
  })).data.data
  courseYes.innerHTML = haveSuppleCourseCount
  courseNo.innerHTML = allCourse - haveSuppleCourseCount
}

getTeacherName()

getClassSuppleInformation()

// 选择课程展示已选课学生
list.addEventListener('click', async (e) => {
  const element = e.target
  if (element.tagName !== 'TD') {
    return
  }
  // 去除上一个选中提示
  const oldElement = document.querySelector('.grey')
  if (oldElement) {
    oldElementParent = oldElement.parentNode
    for (let i = 0; i < oldElementParent.children.length; i++) {
      oldElementParent.children[i].classList.remove('grey')
    }
  }
  // 添加选中提示
  const elementParent = element.parentNode
  for (let i = 0; i < elementParent.children.length; i++) {
    elementParent.children[i].classList.add('grey')
  }
  const classId = nowClassId = elementParent.dataset.id
  classCredit = elementParent.children[2].innerHTML      // 记录选中课程学分
  const students = await getCourseStudent(classId)
  const htmlStr = students.map((e) => {
    const degreeValue = e.degreeId
    let degree
    if (degreeValue.indexOf("1") !== -1) {
      degree = '专科'
    } else if (degreeValue.indexOf("2") !== -1) {
      degree = ' 本科'
    } else if (degreeValue.indexOf("3") !== -1) {
      degree = ' 硕士研究生'
    } else if (degreeValue.indexOf("4") !== -1) {
      degree = ' 博士研究生'
    } else if (degreeValue.indexOf("5") !== -1) {
      degree = ' 博士后研究生'
    }
    return `
      <tr data-id="${e.id}">
        <td>${e.id}</td>
        <td>${e.name}</td>
        <td>${degree}</td>
        <td><input type="text"></td>
        <td class="creditGrade">${e.cpa === -1 ? '' : e.cpa}</td>
      </tr>
    `
  }).join('')
  studentsList.innerHTML = htmlStr
  getPageSum()    //获取页数
})

// 赋予绩点
studentsList.addEventListener('change', (e) => {
  const element = e.target
  if (!element.type === 'text') {
    return
  }
  const studentId = element.parentNode.parentNode.dataset.id
  const credit = +element.value
  if (credit < 0 || credit > 5) {
    alert('绩点不符合规范')
    return
  }
  axios({
    url: 'http://127.0.0.1:8080/Teacher/SupplementalGrades/uploadCredit',
    method: 'POST',
    params: {
      studentId,
      classId: nowClassId,
      credit,
      classCredit
    }
  }).then(() => {
    // 自动填充学分绩点
    document.querySelector(`.studentsList tr[data-id="${studentId}"] td:nth-child(5)`).innerHTML = parseFloat((credit * classCredit).toFixed(2))
  })
})


// 表格控件监听
async function getPageSum() {
  maxPage = (await axios({
    url: 'http://127.0.0.1:8080/Teacher/SupplementalGrades/getPageSum',
    params: {
      id: nowClassId,
      pageNumber
    }
  })).data.data
  allPage.innerHTML = maxPage
}

first.addEventListener('click', () => {
  page = 1
  getCourseStudent(nowClassId)
})

last.addEventListener('click', () => {
  if (page > 1) {
    --page
    getCourseStudent(nowClassId)
  }
})

next.addEventListener('click', () => {
  getPageSum()
  if (page < maxPage) {
    ++page
    getCourseStudent(nowClassId)
  }
})

theLast.addEventListener('click', () => {
  page = maxPage
  getCourseStudent(nowClassId)
})

pageSum.addEventListener('change', () => {
  pageNumber = pageSum.value
  page = 1
  getCourseStudent(nowClassId)
  getPageSum()
})

paged.addEventListener('change', () => {
  page = paged.value
  // 限定page范围
  page = page > maxPage ? maxPage : page
  page = page < 1 ? 1 : page
  getCourseStudent(nowClassId)
})
